package reflex.utils;

import java.lang.reflect.Method;

public class MethodUtils {

    public static Object invokeMethod(Object obj,String methodName,Object... params){
       Class<?> objClass = obj.getClass();
        Object result = null;
       try {
           long startTime = System.currentTimeMillis();
           System.out.println("类："+objClass.getName());
           System.out.println("方法："+methodName);
           if (params == null || params.length == 0){
               Method method = objClass.getDeclaredMethod(methodName);
               method.setAccessible(true);
                result = method.invoke(obj);

           }else {
               int size = params.length;
               Class<?>[] classes = new Class[size];
               Object[] paramValues = new Object[size];
               for (int i = 0; i < params.length; i++){
                   classes[i] = params[i].getClass();
                   paramValues[i] = params[i];
               }
               Method method = objClass.getDeclaredMethod(methodName,classes);
               method.setAccessible(true);
               result = method.invoke(obj,paramValues);

           }

       }catch (Exception e){
           e.printStackTrace();
       }
        System.out.println("返回值："+result);
        return result;
    }

}
